﻿package cn.flashk.map
{
    import cn.flashk.events.*;
    import cn.flashk.image.*;
    import cn.flashk.net.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class ObjectDisplay extends Bitmap
    {
        private var fileURL:String;
        private var isInit:Boolean = false;
        private var sortX:Number;
        private var sortY:Number;
        private var sortRotation:Number;
        private var cutBD:BitmapData;
        private var alphaNum:uint = 3.13734e+009;

        public function ObjectDisplay()
        {
            LoaderManager.getInstance().addEventListener(LoaderManagerEvent.COMPLETE, checkFile);
            MapEngine.getInstance().backgroundLayer.addEventListener("gridAllLoaded", drawToBG);
            return;
        }// end function

        public function init(param1:XML) : void
        {
            fileURL = param1.@url;
            this.x = param1.@x;
            this.y = param1.@y;
            sortX = param1.@sortX;
            sortY = param1.@sortY;
            sortRotation = param1.@sortRotation;
            LoaderManager.getInstance().load(fileURL, "ObjectDisplay");
            return;
        }// end function

        public function get depthX() : Number
        {
            return this.x + sortX;
        }// end function

        public function get depthY() : Number
        {
            return this.y + sortY;
        }// end function

        private function checkFile(event:LoaderManagerEvent) : void
        {
            if (isInit == true)
            {
                return;
            }
            if (event.fileURL == fileURL)
            {
                new ImageDecode(event.data, initBD);
                isInit = true;
            }
            return;
        }// end function

        private function initBD(param1:Bitmap) : void
        {
            this.bitmapData = param1.bitmapData.clone();
            return;
        }// end function

        private function drawToBG(event:Event) : void
        {
            setTimeout(drawToBGMain, Math.random() * 900);
            return;
        }// end function

        private function drawToBGMain() : void
        {
            var _loc_5:uint = 0;
            var _loc_7:int = 0;
            this.alpha = 1;
            var _loc_1:* = this.bitmapData;
            MapEngine.getInstance().backgroundLayer.copyObjectView(_loc_1, new Rectangle(0, 0, _loc_1.width, _loc_1.height), new Point(this.x, this.y));
            if (this.parent != null)
            {
            }
            cutBD = new BitmapData(this.width, sortY, true, 0);
            var _loc_2:* = new BitmapData(this.width, sortY, true, alphaNum);
            cutBD.copyPixels(_loc_1, new Rectangle(0, 0, cutBD.width, cutBD.height), new Point(0, 0));
            var _loc_3:* = cutBD.width;
            var _loc_4:* = cutBD.height;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_4)
            {
                
                _loc_7 = 0;
                while (_loc_7 < _loc_3)
                {
                    
                    _loc_5 = cutBD.getPixel32(_loc_7, _loc_6);
                    if (_loc_5 > 4278190080 || _loc_5 < 872415231)
                    {
                    }
                    else
                    {
                        cutBD.setPixel32(_loc_7, _loc_6, 0);
                    }
                    _loc_7++;
                }
                _loc_6++;
            }
            cutBD.copyPixels(cutBD, new Rectangle(0, 0, cutBD.width, cutBD.height), new Point(0, 0), _loc_2);
            _loc_2.dispose();
            this.bitmapData.dispose();
            this.bitmapData = cutBD;
            return;
        }// end function

    }
}
